package cn.Mmovie.service.impl;

import cn.Mmovie.bean.Cinema;
import cn.Mmovie.dao.CinemaDao;
import cn.Mmovie.dao.Impl.CinemaDaoImpl;
import cn.Mmovie.service.CinemaService;
import cn.Mmovie.util.PageUtil;

import java.util.List;

public class CinemaServiceImpl implements CinemaService {
    private CinemaDao cinemaDao=new CinemaDaoImpl();

    @Override
    public Integer addCinema(Cinema cinema) {
        Integer res=-1;
        if (selectValidateName(cinema.getName())==null){
            res=cinemaDao.insert(cinema);
        }
        return res;
    }

    @Override
    public PageUtil<Cinema> page(Integer pageNo, Integer pageSize) {
        PageUtil<Cinema> pageUtil = new PageUtil<>();
        pageUtil.setPageNo(pageNo);
        pageUtil.setPageSize(pageSize);
        pageUtil.setRecordCount(cinemaDao.selectForRecordTotal());
        pageUtil.setItems(cinemaDao.selectForPageItems((pageUtil.getPageNo() - 1) * pageUtil.getPageSize(), pageUtil.getPageSize()));
        pageUtil.setUrl("/admin/cinema_list?pageSize=" + pageUtil.getPageSize());
        return pageUtil;
    }

    @Override
    public Integer deleteCinemaById(Integer id) {
        Integer result=-1;
        if (cinemaDao.selectById(id)!=null){
            result=cinemaDao.delete(id);
        }
        return result;
    }

    @Override
    public Cinema selectValidateName(String name) {
        Cinema cinema = null;
        if (name != null&&name!="") {
            cinema=cinemaDao.selectCinemaByName(name);
        }
        return cinema;
    }

    @Override
    public Cinema queryCinemaById(Integer id) {
        return cinemaDao.selectById(id);
    }

    @Override
    public Integer modifyCinema(Cinema cinema) {
        Integer result=-1;
        if (cinemaDao.selectById(cinema.getId())==null){
            result=cinemaDao.update(cinema);
        }
        return result;
    }

    @Override
    public List<Cinema> queryAllCinema() {
        return cinemaDao.selectAll();
    }
}
